package com.quanyan.travel.web.admin;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.travel.api.req.*;
import com.quanyan.travel.api.resp.RespAppActivityDetail;
import com.quanyan.travel.api.resp.RespProductMsg;
import com.quanyan.travel.api.resp.RespWebActivityDetail;
import com.quanyan.travel.entity.TbCity;
import com.quanyan.travel.entity.TbOutdoorCategory;
import com.quanyan.travel.entity.TbOutdoorDestination;
import com.quanyan.travel.entity.vo.Banner;
import com.quanyan.travel.entity.vo.Category;
import com.quanyan.travel.entity.vo.MasterUser;
import com.quanyan.travel.service.TravelAdminService;
import com.quanyan.travel.service.TravelDetailService;
import com.quanyan.travel.service.TravelOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zyq
 * Date: 2016/6/2 0002
 * Time: 10:25
 */
@RestController
@RequestMapping("/admin/api/travel")
public class AdminController {

    @Autowired
    TravelOrderService travelOrderService;

    @Autowired
    TravelAdminService adminService;

    @Autowired
    TravelDetailService detailService;



    //查询banner图
    @RequestMapping(value = "/allBanners",method = RequestMethod.POST)
    public APIResponse<List<Banner>> allBanner(){
        return adminService.allBanner();
    }

    //删除banner图
    @RequestMapping(value = "/deleteBanner",method = RequestMethod.POST)
    public APIResponse<Integer> deleteBanner(@RequestBody Map<String, Integer> map){
        return adminService.deleteBanner(map.get("id"));
    }

    //新增banner图
    @RequestMapping(value = "/addBanner",method = RequestMethod.POST)
    public APIResponse<Integer> addBanner(@RequestBody @Valid Banner banner,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.addBanner(banner);
    }

    /**
     * 新增banner图 —— 查询可推荐的活动
     */
    @RequestMapping(value = "/selectActivityForBanner",method = RequestMethod.POST)
    public APIResponse<Banner> selectActivityForBanner(@RequestBody Map<String,String> map,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.selectActivityForBanner(map.get("name"));
    }

    /**
     * 已经推荐的活动数量
     */
    @RequestMapping(value = "/selectRecommendNum",method = RequestMethod.GET)
    public APIResponse<Integer> selectRecommendNum() {
        return adminService.selectRecommendNum();
    }

    /**
     * 查询所有城市
     *
     * */
    @RequestMapping(value = "/allCities",method = RequestMethod.POST)
    public APIResponse<List<TbCity>> allCities(){
        return adminService.queryAllCity();
    }

    /**
     * 创建领队
     *
     * */
    @RequestMapping(value = "/createMaster",method = RequestMethod.POST)
    public APIResponse<?> createMaster(@RequestBody @Valid ReqMaster req,BindingResult bindingResult){
        return adminService.createMaster(req);
    }

    /**
     * 所有玩法列表
     *
     * */
    @RequestMapping(value = "/getAllCategories",method = RequestMethod.POST)
    public APIResponse<List<Category>> getAllCategories(){
        return adminService.allCategories();
    }


    /**
     * 玩法插入
     *
     * */
    @RequestMapping(value = "/insertOutdoorCategory",method = RequestMethod.POST)
    public APIResponse<Integer> insertOutdoorCategory(@RequestBody @Valid TbOutdoorCategory req,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        adminService.insertOutdoorCategory(req);
        return APIResponse.returnSuccess();
    }

    /**
     * 玩法修改
     *
     * */
    @RequestMapping(value = "/updateOutdoorCategory",method = RequestMethod.POST)
    public APIResponse<Integer> updateOutdoorCategory(@RequestBody @Valid TbOutdoorCategory req,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        adminService.updateOutdoorCategory(req);
        return APIResponse.returnSuccess();
    }

    /**
     * 查询所有目的地列表
     *
     * */
    @RequestMapping(value = "/getAllDestinations",method = RequestMethod.POST)
    public APIResponse<?> getAllDestinations(@RequestBody @Valid ReqAllDestination allDestination,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.allDestinations(allDestination);
    }


    /**
     * 获取活动报名信息和活动本身信息
     *
     * */
    @RequestMapping(value = "/activityMsg",method = RequestMethod.POST)
    public APIResponse<?> activityMsg(){
        return adminService.activityMsg();
    }


    /**
     * 目的修改
     *
     * */
    @RequestMapping(value = "/updateDestination",method = RequestMethod.POST)
    public APIResponse<Integer> updateDestination(@RequestBody @Valid TbOutdoorDestination req,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        adminService.updateDestination(req);
        return APIResponse.returnSuccess();
    }


    /**
     * 创建目的地
     *
     * */
    @RequestMapping(value = "/createDestination",method = RequestMethod.POST)
    public APIResponse<?> createDestination(@RequestBody @Valid ReqDestination req,BindingResult bindingResult){
        return adminService.createDestination(req);
    }



    /**
     *
     * 查询领队用户列表
     *
     * */
    @RequestMapping(value = "/getMasterUser",method = RequestMethod.POST)
    public APIResponse<PageObj<List<MasterUser>>> getMasterUser(@RequestBody @Valid ReqMasterList reqMasterList,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.getMasterUser(reqMasterList);
    }

    /**
     *
     * 查询用户,根据电话
     *
     * */
    @RequestMapping(value = "/getUserByMobile",method = RequestMethod.POST)
    public APIResponse getUserByMobile(@RequestBody Map<String, Object> map,BindingResult bindingResult){
        if (StringUtils.isEmpty(map.get("mobile"))) {
            return APIResponse.returnFail("mobile不能为空");
        }
        return adminService.getUserByMobile(map.get("mobile")+"");
    }

    /**
     *
     * 创建领队
     *
     * */
    @RequestMapping(value = "/createMasterUser",method = RequestMethod.POST)
    public APIResponse createMasterUser(@RequestBody @Valid ReqCreateMasterUser reqCreateMasterUser,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.createMasterUser(reqCreateMasterUser);
    }

    /**
     * 修改领队
     */
    @RequestMapping(value = "/updateMasterUser",method = RequestMethod.POST)
    public APIResponse updateMasterUser(@RequestBody @Valid MasterUser masterUser ,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.updateMasterUser(masterUser);
    }

    /**
     * 设置优先级
     */
    @RequestMapping(value = "/setBannerPriority",method = RequestMethod.POST)
    public APIResponse<?> setBannerPriority(@RequestBody @Valid ReqBannerPriorityList priorityList , BindingResult result){
        if (result.hasErrors()){
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return adminService.setBannerPriority(priorityList);
    }

    /**
     * 编辑banner图
     */
    @RequestMapping(value = "/editBanner",method = RequestMethod.POST)
    public APIResponse<?> editBanner(@RequestBody @Valid Banner banner, BindingResult result){
        if (result.hasErrors()){
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return adminService.editBanner(banner);
    }


    /**
     *
     * 领队移除
     *
     * */
    @RequestMapping(value = "/delMasterUser",method = RequestMethod.POST)
    public APIResponse delMasterUser(@RequestBody Map<String, Integer> map){
        String masterId = map.get("masterId")+"";
        if (StringUtils.isEmpty(masterId)) {
            return APIResponse.returnFail("masterId不能为空");
        }
        return adminService.delMasterUser(Integer.parseInt(masterId));
    }

    /**
     * 添加场次
     *
     * */

    @RequestMapping(value = "/createProduct",method = RequestMethod.POST)
    public APIResponse<?> createProduct(@RequestBody @Valid ReqCreateProduct req,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.createProduct(req);
    }


    /**
     *
     *  活动列表
     * */

    @RequestMapping(value = "/getActivityList", method = RequestMethod.POST)
    public APIResponse<?> getActivityList(@RequestBody @Valid ReqActivityList req,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.getActivityList(req);
    }

    /**
     * 查询非季度活动下的场次信息
     *
     * */

    @RequestMapping(value = "/getProductListByActivityId", method = RequestMethod.POST)
    public APIResponse<RespProductMsg> getProductListByActivityId(@RequestBody Map<String, Integer> map) {
        if (null==map.get("activityId")) {
            return APIResponse.returnFail("activityId不能为空");
        }
        return adminService.getProductListByActivityId(map.get("activityId"));
    }


    /**
     * 活动下架,未报名开始的活动才能下架
     *
     * */
    @RequestMapping(value = "/activityUndercarriage",method = RequestMethod.POST)
    public APIResponse<Integer> activityUndercarriage(@RequestBody Map<String, Object> map){
        if (null==map.get("activityId")) {
            return APIResponse.returnFail("activityId不能为空");
        }
        String id = map.get("activityId")+"";
        return adminService.activityUndercarriage(Integer.parseInt(id));
    }
    /**
     * 活动详情
     *
     * */
    @RequestMapping(value = "/activityDetail",method = RequestMethod.POST)
    public APIResponse<RespWebActivityDetail> activityDetail(@RequestBody Map<String, Integer> map){
        if (null==map.get("activityId")) {
            return APIResponse.returnFail("activityId不能为空");
        }
        return adminService.activityDetail(map.get("activityId"));
    }
    /**
     * 活动下价格展示
     *
     * */
    @RequestMapping(value = "/productDetailList",method = RequestMethod.POST)
    public APIResponse<RespAppActivityDetail> productDetailList(@RequestBody Map<String, Integer> map){
        return adminService.getProductList(map.get("activityId"));
    }

    /**
     * 创建活动
     *
     **/
    @RequestMapping(value = "/createActivity",method = RequestMethod.POST)
    public APIResponse<Integer> createActivity(@RequestBody @Valid ReqCreateActivity req,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        if (null!=req.getReqCreateProducts()) {
            for (ReqCreateProduct product : req.getReqCreateProducts()) {
                if (null == product.getStartTime()) {
                    return APIResponse.returnFail("活动场次的开始时间不能为空");
                }
                if (null == product.getSignStartTime()) {
                    return APIResponse.returnFail("活动场次的开始报名时间不能为空");
                }
                if (null == product.getSignEndTime()) {
                    return APIResponse.returnFail("活动场次的报名结束时间不能为空");
                }
            }
        }
        return adminService.createActivity(req);
    }

    /**
     * 活动编辑
     */
    @RequestMapping(value = "/updateActivity",method = RequestMethod.POST)
    public APIResponse<Integer> updateActivity(@RequestBody @Valid ReqCreateActivity req,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        if (null!=req.getReqCreateProducts()) {
            for (ReqCreateProduct product : req.getReqCreateProducts()) {
                if (null == product.getStartTime()) {
                    return APIResponse.returnFail("活动场次的开始时间不能为空");
                }
                if (null == product.getSignStartTime()) {
                    return APIResponse.returnFail("活动场次的开始报名时间不能为空");
                }
                if (null == product.getSignEndTime()) {
                    return APIResponse.returnFail("活动场次的报名结束时间不能为空");
                }
            }
        }
        return adminService.updateActivity(req);
    }


    /**
     * 订单列表
     *
     **/
    @RequestMapping(value = "/queryOrderList",method = RequestMethod.POST)
    public APIResponse<?> queryOrderList(@RequestBody @Valid ReqOrderList req,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.travelOrderList(req);
    }

    /**
     * 订单详情
     *
     **/
    @RequestMapping(value = "/queryOrderDetail",method = RequestMethod.POST)
    public APIResponse<?> queryOrderDetail(@RequestBody @Valid Map map,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return adminService.travelOrderDetail((String) map.get("orderNo"));
    }

    /**
     * 申请退款的订单,调起退款逻辑
     *
     **/
    @RequestMapping(value = "/passRefundOrder",method = RequestMethod.POST)
    public APIResponse<?> passRefundOrder(@RequestBody @Valid Map map,BindingResult bindingResult) throws Exception {
        if (null == map.get("orderNo")) {
            return APIResponse.returnFail("参数不合法");
        }
        return travelOrderService.passRefundOrder((String) map.get("orderNo"));
    }


    /**
     * 申请退款的订单,调起退款逻辑
     *
     **/
    @RequestMapping(value = "/refundAllOrder",method = RequestMethod.POST)
    public APIResponse<?> refundAllOrder(@RequestBody @Valid Map map,BindingResult bindingResult) throws Exception {
        if (null == map.get("orderNo")) {
            return APIResponse.returnFail("参数不合法");
        }
        return travelOrderService.refundAllOrder((String) map.get("orderNo"));
    }


    /**
     * 订单退款预览
     *
     **/
    @RequestMapping(value="/orderRefundPreview", method = RequestMethod.POST)
    public APIResponse<?> orderRefundPreview(@RequestBody  Map<String, Object> map, HttpServletRequest request) throws Exception {
        if (null == map.get("orderNo")) {
            return APIResponse.returnFail("参数不合法");
        }
        try{
            return travelOrderService.preRefundOrder(map.get("orderNo")+"");
        }catch (Exception e) {
            return APIResponse.returnFail("服务器异常");
        }
    }

    /**
     * 清空对应key的redis缓存
     *
     **/
    @RequestMapping(value="/clearCacheByKey", method = RequestMethod.GET)
    public APIResponse<?> clearCacheByKey() throws Exception {
        try{
            return adminService.clearCache();
        }catch (Exception e) {
            return APIResponse.returnFail("服务器异常");
        }
    }

}
